package com.ckd.flyingtrip.shared.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.BaseActivity;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.shared.adapter.SharedCarOrderAdapter;
import com.ckd.flyingtrip.shared.bean.SharedCarOrderBean;
import com.ckd.flyingtrip.utils.ItemDecoration;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SharedCarOrderActivity extends BaseActivity {


    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.activity_sharedcar_order_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.activity_sharedcar_order_no)
    TextView no;
    @BindView(R.id.activity_sharedcar_order_smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    private SharedCarOrderAdapter adapter;
    private List<SharedCarOrderBean.DataBean.RowsBean> list = new ArrayList<>();

    private int pageNum = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharedcar_order);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();

    }

    private void initView() {
        title.setText("共享订单");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ItemDecoration(0, 0, 0, 30));
        adapter = new SharedCarOrderAdapter(this, list);
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.GET, Constants.shared_triplist + "?pageNum=" + pageNum + "&pageSize=10", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("共享用户行程列表", s);
                SharedCarOrderBean bean = new Gson().fromJson(s, SharedCarOrderBean.class);
                if (bean.getCode().equals("200")) {
                    list.addAll(bean.getData().getRows());
                    if (adapter != null)
                        adapter.notifyDataSetChanged();

                    if (list.size() == Integer.parseInt(bean.getData().getTotal()))
                        smartRefreshLayout.setEnableLoadMore(false);

                    if (list.size() == 0) {
                        recyclerView.setVisibility(View.GONE);
                        no.setVisibility(View.VISIBLE);
                        smartRefreshLayout.setEnableLoadMore(false);
                    } else {
                        recyclerView.setVisibility(View.VISIBLE);
                        no.setVisibility(View.GONE);
                    }
                } else {
                    Tools.toast(bean.getMessage());
                }
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishLoadMore();
                    smartRefreshLayout.finishRefresh();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishLoadMore();
                    smartRefreshLayout.finishRefresh();
                }
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> mHeader = new HashMap<String, String>();
                mHeader.put("TOKEN_XCX_SHARE", SharedCarActivity.token);
                return mHeader;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    private void initListener() {

        adapter.setOnItemClickListener(new SharedCarOrderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(SharedCarOrderActivity.this, SharedCarOrderDetailActivity.class);
                intent.putExtra("id", list.get(position).getId());
                startActivity(intent);
            }
        });

        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                initData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                smartRefreshLayout.setEnableLoadMore(true);
                list.clear();
                initData();
            }
        });
    }

    @OnClick(R.id.toolber_color_back)
    public void onViewClicked() {
        finish();
    }
}
